#include "command.h"

int handler_cd(int sockfd, data msg, char *wk_path, char *name, MYSQL *mysql, char *fixed_path){
    char curr[1024] = {0};
    strcpy(curr, fixed_path);
    //获取cd的目录名
    // char cd_name[100] = {0};
    // strcat(cd_name, msg.args);
    // cd_name[strlen(msg.args)] = '\0';
    //定义一个结构体接收cd的客户端路径
    //data curr_msg;
    memset(&msg, 0, sizeof(data)); 
    //接收带有路径的结构体
    recv(sockfd, &msg, sizeof(data), MSG_WAITALL);
    //printf("接收到的路径 = %s\n", msg.args);
    char path[1024] = {0};
    strcpy(path, msg.args);
    //strcat(path, "/");
    //strcat(path, cd_name);

    //mysql_query(mysql, "select user_id form user where user_name = *name");
    char sql_buf[1024] = {0};
    //snprintf(sql_buf, sizeof(sql_buf), "select count(file_id) from file where file_path = '%.*s' and user_id = (select user_id from user where user_name = '%.*s')", 255,name, 255, path);
    snprintf(sql_buf, sizeof(sql_buf), "select count(file_id) from file where user_id = (select user_id from user where user_name = '%.*s') and file_path = '%.*s'", 255,name, 255, path);
    //进行查询
    mysql_query(mysql, sql_buf);
    //获取返回值
    MYSQL_RES *result = mysql_store_result(mysql);
    //处理查询不到任何东西的逻辑，发送标记位，退出
    if(result == NULL || mysql_num_rows(result) == 0){
        int flag = 0;
        send(sockfd, &flag, sizeof(int), MSG_NOSIGNAL);
        return 0;
    }
    //从返回值中取出返回结果存入buf数组中
    char buf[512] = {0};
    MYSQL_ROW row;
    while((row = mysql_fetch_row(result))){
        for (int i = 0; i < mysql_num_fields(result); i++){
            strncat(buf, row[i], strlen(row[i]));
        }
    }
    //释放结果集
    mysql_free_result(result);
    //设置预处理的sql
    // MYSQL_STMT *stmt = mysql_stmt_init(mysql);
    // mysql_stmt_prepare(stmt, sql_buf, strlen(sql_buf));

    //放入参数
    // MYSQL_BIND bind[2];
    // memset(bind, 0, sizeof(bind));
    //设置第一个参数的类型为字符串。
    //MYSQL_TYPE_STRING 是一个枚举值，告诉 MySQL 这个参数是一个字符串类型。
    // bind[0].buffer_type = MYSQL_TYPE_STRING;
    // bind[0].buffer = (char *)name;
    // bind[0].buffer_length = strlen(name);
    // bind[1].buffer_type = MYSQL_TYPE_STRING;
    // bind[1].buffer = (char *)path;
    // bind[1].buffer_length = strlen(path);

    //调用执行
    // mysql_stmt_execute(stmt);

//     //绑定结果集
//     MYSQL_BIND bind_result[2];
//     char result_column[2][30];
//     memset(bind_result, 0,sizeof(bind_result));
//     memset(result_column, 0, sizeof(result_column));

//     for(int k=0; k<2; k++){
//        bind_result[k].buffer_type = MYSQL_TYPE_STRING;
//        //将每个bind_result元素的buffer成员设置为result_column数组的对应元素的地址。
//        bind_result[k].buffer = result_column[k];
//        bind_result[k].buffer_length = sizeof(result_column[k]);
//     }
//    mysql_stmt_bind_result(stmt, bind_result);
    // MYSQL_RES *result = mysql_store_result(mysql);
    // char buf[100] = {0}; //用于存储查询到的数据
    // MYSQL_ROW row; 
    // while((row = mysql_fetch_row(result))){ //从结果集中取出一行，自动往下一行
    //     for(int i = 0; i < mysql_num_fields(result); i++){ //遍历列
    //         //strncat(buf, " \t", 4);
    //         strncat(buf, row[i], strlen(row[i]));
    //     }
    // }
    // mysql_free_result(result);
    int count_num = atoi(buf);
    //printf("count_num = %d\n", count_num);
    send(sockfd, &count_num, sizeof(int), MSG_NOSIGNAL);
    if(count_num > 0){
        char *p = "/";
        strcat(curr, p);
        strcat(curr, path);
        strcpy(wk_path, curr);
        return 0;
    }
    return 0;
}